package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.pojo.Address;
import com.atguigu.service.AddressService;
import com.atguigu.vo.PageResult;
import com.atguigu.vo.QueryPageBean;
import com.atguigu.vo.Result;
import org.springframework.web.bind.annotation.*;

/**
 * @program: meinian_parent
 * @author: Mr.Zhuo
 * @create: 2022-02-22 14:57
 **/
@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    AddressService addressService;

    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = addressService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());

        return pageResult;
    }

    @PostMapping("/addAddress")
    public Result addAddress(@RequestBody Address address) {
        if (address.getAddressName() == null || address.getLat() == null) {
            return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
        } else {
            addressService.addAddress(address);
            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        }
    }

    @GetMapping("/deleteById")
    public Result deleteById(@RequestParam("id")Integer id){
        try {
            addressService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_ADDRESS_FAIL);
        }
    }
}
